﻿Imports System.Data.SqlClient

Public Class frmDeleteClaims

    Sub Clear()
        Me.txtClaimNo.Clear()
        Me.txtClientID.Clear()
        Me.txtClientName.Clear()
        Me.txtInsuranceCompany.Clear()
        Me.txtPolicyNo.Clear()
        Me.txtMemberName.Clear()
        Me.txtMedIDPlateNo.Clear()
        Me.CombCarMake.SelectedItem = Nothing
        Me.txtInsurCompClaimNo.Clear()
        Me.txtClaimAmount.Clear()
        Me.DTPDateOfLoss.Value = Now
        Me.DTPDateOfReceive.Value = Now
        Me.DTPDateOfSubmission.Value = Now
        Me.DTPDateOfDeadline.Value = Now
        Me.txtClaimNotes.Clear()
        Me.txtStatus.Clear()
    End Sub

    Private Sub txtClaimNo_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtClaimNo.KeyUp
        If e.KeyCode = Keys.Enter Then
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Select ClientID,CLientName,Status,InsuranceCompany,PolicyNo,MembName,MedIDCarNo,ClaimAmount," & _
                                          "IsNull(CarsMake,N'') CarsMake,DateOfLoss,DateOfReceive,DateOfSubmission,DateOfDeadline,IsNull(InsurCompClaimNo,N'') InsurCompClaimNo," & _
                                          "IsNull(Notes,N'') Notes,SavedUser,SavedDate From Claims Where ClaimNo=N'" & Me.txtClaimNo.Text.Trim & "'", cnn)
                Dim Reader As SqlDataReader
                Dim Status As String

                cnn.Open()
                Reader = cmd.ExecuteReader
                While Reader.Read
                    Me.txtClientID.Text = Reader.Item("ClientID")
                    Me.txtClientName.Text = Reader.Item("ClientName")
                    Me.txtInsuranceCompany.Text = Reader.Item("InsuranceCompany")
                    Me.txtPolicyNo.Text = Reader.Item("PolicyNo")
                    Me.txtMemberName.Text = Reader.Item("MembName")
                    Me.txtMedIDPlateNo.Text = Reader.Item("MedIDCarNo")
                    Me.CombCarMake.Text = Reader.Item("CarsMake")
                    Me.txtInsurCompClaimNo.Text = Reader.Item("InsurCompClaimNo")
                    Me.txtClaimAmount.Text = CDbl(Reader.Item("ClaimAmount")).ToString("N2")
                    Me.DTPDateOfLoss.Text = CDate(Reader.Item("DateOfLoss"))
                    Me.DTPDateOfReceive.Text = CDate(Reader.Item("DateOfReceive"))
                    Me.DTPDateOfSubmission.Text = CDate(Reader.Item("DateOfSubmission"))
                    Me.DTPDateOfDeadline.Text = CDate(Reader.Item("DateOfDeadline"))
                    Me.txtClaimNotes.Text = Reader.Item("Notes")

                    Status = Reader.Item("Status")
                    Me.txtStatus.Text = Reader.Item("Status")
                End While
                cnn.Close()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub btnClos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClos.Click
        Me.Close()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Me.Cursor = Cursors.Default

            If (MsgBox("confirm Delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Delete From Claims Where ClaimNo =N'" & Me.txtClaimNo.Text.Trim & "'", cnn)

                cnn.Open()
                cmd.ExecuteNonQuery()
                cnn.Close()

                MsgBox("Delete successfully", MsgBoxStyle.Critical, Me.Text)
                Clear()
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class